package com.kgd.news.controller.admin;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.upload.UploadFile;
import com.kgd.news.entity.Media;
import com.kgd.news.entity.Smallapp;
import com.kgd.news.utils.Api;
import com.kgd.news.utils.BaseController;
import com.kgd.news.utils.StringUtilsWx;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Email: huangsanm@foxmail.com
 * @Date: 2017/10/9
 * @Time: 下午3:03
 */
public class SmallAppController extends BaseController {


    @Override
    public void list() {
        String categoryid = "55";
        String sql = "from smallapp where 1=1 ";
        String type = "smallapp";
        if (!StringUtilsWx.isEmpty(categoryid)) {
            if ("-1".equals(categoryid)) {
                sql += " and categoryid is null";
            } else {
                sql += " and categoryid =" + categoryid;
            }
            type += categoryid;
        }
        Page<Smallapp> data = Smallapp.dao.paginate(getPageIndex(type), PAGE_SIZE, "select *", sql);
        setAttr("data", data);
        setAttr("categoryid", categoryid);
        render("smallapp_list.html");
    }

    @Override
    public void edit() {
        Integer id = getParaToInt("id");
        Smallapp img = Smallapp.dao.findById(id);
        setAttr("categoryid", getPara("categoryid"));
        setAttr("smallapp", img);
        render("smallapp_edit.html");
    }

    @Override
    public void save() {
        UploadFile uploadFile = getFile("cover");
        String filePath = "";
        if (uploadFile != null && !StringUtilsWx.isEmpty(uploadFile.getFileName())) {
            filePath = getFileUploadPath(uploadFile);
        }
        UploadFile uploadFile1 = getFile("icourl");
        String filePath1 = "";
        if (uploadFile1 != null && !StringUtilsWx.isEmpty(uploadFile1.getFileName())) {
            filePath1 = getFileUploadPath(uploadFile1);
        }
        Smallapp us = getModel(Smallapp.class, "smallapp");
//        根据type来判断图标有没有
//        if (us.getType() == 56) {
//            if (StringUtils.isEmpty(us.getIcourl())&&StringUtils.isEmpty(filePath1)) {
//                alert("");
//                return;
//            }
//        }else if(us.getType() == 57||us.getType() == 58) {
//            if (StringUtils.isEmpty(us.getIcourl())&&StringUtils.isEmpty(filePath1)) {
//                alert("");
//                return;
//            }
//        }
        if (!StringUtilsWx.isEmpty(filePath)) {
            us.setCover(filePath);
        }
        if (!StringUtilsWx.isEmpty(filePath1)) {
            us.setIcourl(filePath1);
        }
        if (us != null) {
            us.setCategoryid(55);
            if (us != null && us.getId() != null) {
                us.update();
            } else if (us != null && us.getId() == null) {
                us.save();
            }
            CacheKit.removeAll("smallapp");
        }
        setAttr("categoryid", getPara("categoryid"));
        list();
    }

    public void alert(String message) {
        try {
            PrintWriter out = getResponse().getWriter();
            out.print("<script>alert('say what you want to say');</script>");
            out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void del() {
        String ids = getPara("ids");
        Db.update("delete from smallapp where id in (" + ids + ")");
        setAttr("categoryid", getPara("categoryid"));
        CacheKit.removeAll("smallapp");
        list();
    }

    public void uplodImg() {
        UploadFile uploadFile = getFile();
        String filePath = "";
        if (uploadFile != null && !StringUtilsWx.isEmpty(uploadFile.getFileName())) {
            filePath = getFileUploadPath(uploadFile);
        }
        Media m = new Media();
        m.setImg(filePath);
        m.setType(1);
        if (m.save()) {
            renderJson(new Api(Api.S_200, m).get());
        } else {
            renderJson(new Api(Api.E_201, "'").get());
        }
    }


}
